/*------------------+
|	Tile Manager	|
|	------------	|
|	Class that		|
|	manages tiles	|
+------------------*/

#include "TileManager.h"

typedef vector<Tile*>::iterator iterator;

// Get the instance
TileManager* TileManager::GetInstance(void)
{
	static TileManager instance;
	return &instance;
}

// Add a tile
void TileManager::AddTile(Tile* tile)
{
	if( tile == nullptr )
		return;

	Tiles.push_back(tile);
	tile->AddRef();
}

// Remove a tile
void TileManager::RemoveTile(Tile* tile)
{
	if( tile == nullptr )
		return;

	iterator iter;
	for(iter = Tiles.begin(); iter != Tiles.end(); ++iter)
	{
		if( (*iter) == tile )
		{
			iter = Tiles.erase(iter);
			tile->Release();
			break;
		}
	}
}

// Draw all the tiles
void TileManager::RenderTiles(void)
{
	iterator iter;
	for(iter = Tiles.begin(); iter != Tiles.end(); ++iter)
	{
		(*iter)->Render();
	}
}

// Clean out all the tiles
void TileManager::RemoveAllTiles(void)
{
	for(unsigned int i = 0; i < Tiles.size(); ++i)
		Tiles[i]->Release();

	Tiles.clear();
}